<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>车类型编辑</title>
    <link href="./css/bootstrap.css" rel="stylesheet">

    <script src="./js/jq.js"></script>
    <script src="./js/bootstrap.js"></script>
    <script>
        function getQueryString(name) {
            const URL = new URLSearchParams(window.location.search)
            let val = URL.get(name)
            return val
        }

        let tid = getQueryString('tid')

        $(function (){
            if(tid != null) {
                $.getJSON("http://localhost:8080/cartype/one/" + tid, function (res) {
                    console.log(res)
                    $("#tid").val(tid)
                    $("#name").val(res.data.name)
                })
            }else{
                $("#tidbox").hide()
            }
        })

        function save(){
            if(tid != null){
                //修改
                $.post("http://localhost:8080/cartype/update",{
                    tid:tid,
                    name:$("#name").val()
                },function (res){
                    console.log(res)
                    if(res.code == 200) {
                        alert('修改成功')
                    }
                })
            }else{
                //新增
                $.post("http://localhost:8080/cartype/add",{
                    name:$("#name").val()
                },function (res){
                    console.log(res)
                    if(res.code == 200){
                        alert('新增成功,tid:'+res.tid)
                    }
                })
            }
        }
    </script>
</head>
<body>
    <div>
        <div id="tidbox">tid:<input id="tid"/></div>
        类型:<input id="name"/><br/>
        <button onclick="save()">提交</button>
    </div>
</body>
</html>